﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace MenuLib
{
    public enum HotKeyShowMode
    {
        /// <summary>
        ///     the mode supposes, that the fact of item having hot key
        ///     is somehow reflected in item's text, e.g.: "Adds the (n)ew user"
        /// </summary>
        None,
    }

    [Serializable]
    public class MenuHotKeyedItem : MenuItem
    {
        public MenuHotKeyedItem(string name, char hotKey) :
            base(name)
        {
            HotKey = hotKey;
            HotKeyShowMode = HotKeyShowMode.None;
        }

        public MenuHotKeyedItem(string name, string text, char hotKey) :
            base(name, text)
        {
            HotKey = hotKey;
            HotKeyShowMode = HotKeyShowMode.None;
        }

        public char HotKey
        {
            get { return hotKey; }
            protected set { hotKey = char.ToLower(value); }
        }

        public HotKeyShowMode HotKeyShowMode
        {
            get { return hotKeyShowMode; }
            set { hotKeyShowMode = value; }
        }

        public override Menu OwnerMenu
        {
            get
            {
                return base.OwnerMenu;
            }
            set
            {
                base.OwnerMenu = value;
                base.OwnerMenu.SetHotKey(HotKey, this);
            }
        }

        public override void Display()
        {
            switch (this.HotKeyShowMode)
            {
                case HotKeyShowMode.None:
                default:
                    base.Display();
                    break;
            }
        }

        private char hotKey;
        private HotKeyShowMode hotKeyShowMode;

        #region ISerializable Members

        const string hotKeyValueName = "HotKey";
        const string hotKeyShowModeValueName = "HotKeyShowMode";

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue(hotKeyValueName, HotKey);
            info.AddValue(
                hotKeyShowModeValueName,
                Enum.GetName(typeof(HotKeyShowMode), HotKeyShowMode));
        }

        protected MenuHotKeyedItem(SerializationInfo info, StreamingContext context) :
            base(info, context)
        {
            HotKey = info.GetChar(hotKeyValueName);
            HotKeyShowMode = (HotKeyShowMode)Enum.Parse(
                typeof(HotKeyShowMode), info.GetString(hotKeyShowModeValueName));
        }

        #endregion
    }
}
